package com.wedo.auth.controller;

import com.wedo.admin.pojo.Admin;
import com.wedo.auth.config.JwtProperties;
import com.wedo.auth.pojo.UserInfo;
import com.wedo.auth.service.AuthService;
import com.wedo.auth.utils.JwtUtils;
import com.wedo.user.pojo.RequestUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.HashMap;
import java.util.Map;

@Controller
@EnableConfigurationProperties(JwtProperties.class)
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("accredit")
    public ResponseEntity<String> accredit(@RequestBody RequestUser requestUser) {
        String token = null;
        if (requestUser.getType() == 1) {
           // 用户名密码登录
            token = this.authService.accredit(requestUser.getUsername(),requestUser.getPassword());
        } else {
            token = this.authService.accreditByPhone(requestUser.getPhone(),requestUser.getCode());
        }
        if (StringUtils.isBlank(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        return ResponseEntity.ok("{\"WD_TOKEN\":\""+token+"\"}");
    }


    /*
    * {"username":"token"}
    *
    *
    * */

    @GetMapping("verify")
    public ResponseEntity<Map<String,Object>> verify(@RequestHeader("WD_TOKEN")String token) {
        try {
            // 解析jwt工具类使用公钥解析jwt
            UserInfo user = JwtUtils.getInfoFromToken(token,this.jwtProperties.getPublicKey());
            if (user == null) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }

            // 刷新jwt中有效时间
            token = JwtUtils.generateToken(user, this.jwtProperties.getPrivateKey(), this.jwtProperties.getExpire());

            Map<String,Object> map = new HashMap<>();
            map.put("user",user);
            map.put("WD_TOKEN",token);

            return ResponseEntity.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
    }

    @PostMapping("admin")
    public ResponseEntity<String> getAdminJwt(@RequestBody Admin admin) {
        String token = null;
        // 生成jwt
        token = this.authService.getAdminJwt(admin.getAdminName(),admin.getPassword());

        if (StringUtils.isBlank(token)) {
            return ResponseEntity.ok("啥也没有");
        }
        return ResponseEntity.ok("{\"WD_TOKEN\":\""+token+"\"}");
    }
}
